#include "editwidget.h"
#include "ui_editwidget.h"
#include "database/databasequery.h"
#include "database/dbtabuser.h"
#include "dataview/productmodel.h"
#include "database/dbtabproduct.h"
#include "database/dbtabclassifierproduct.h"
#include "dataview/classifierproductmodel.h"
#include "dataview/classifierprtdelegate.h"
#include "dataview/productdelegate.h"
#include "EditorWidgets/producteditor.h"
#include "EditorWidgets/usereditor.h"
#include "EditorWidgets/classifiereditor.h"
#include "tableitemdeleter.h"
#include "common/common.h"
#include <QHeaderView>

#define COLCNT 4

EditWidget::EditWidget(QWidget *parent,QLayout* layout,const QString& columnName,int modelType) :
    QDialog(parent),mModelType(modelType),mColumn(columnName),
    ui(new Ui::EditWidget)
{
    ui->setupUi(this);
    DataBaseQuery query;
    setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowState(Qt::WindowMaximized);

    mTableView = ui->abcTv->getTableView();
//    for(int i = 1;i<6;++i)
//    {
//        QMap<QString,QVariant> map;
//        map.insert(DBTabClassifierProduct::_NAME,"香蕉"+QString::number(i));
//        map.insert(DBTabClassifierProduct::_R1_LOW,5000);
//        map.insert(DBTabClassifierProduct::_R1_HI,10000);
//        map.insert(DBTabClassifierProduct::_R2_LOW,10001);
//        map.insert(DBTabClassifierProduct::_R2_HI,10010);
//        map.insert(DBTabClassifierProduct::_R3_LOW,10011);
//        map.insert(DBTabClassifierProduct::_R3_HI,10020);
//        map.insert(DBTabClassifierProduct::_R4_LOW,10021);
//        map.insert(DBTabClassifierProduct::_R4_HI,10030);
//        query.insert(DBTabClassifierProduct::TAB_NAME,map);
//    }
    switch (modelType) {
    case USERMODEL:
        mModel = new UserModel(COLCNT,columnName,this);
        mTableName = ((UserModel*)mModel)->getTabName();
        setTitleName("用户管理");
        mTableView->setItemDelegate(new UserDelegate(this));
        break;
    case PRODUCTMODEL:
        mModel = new ProductModel(COLCNT,columnName,this);
        mTableName = ((ProductModel*)mModel)->getTabName();
        setTitleName("产品管理");
        mTableView->setItemDelegate(new ProductDelegate(this));
        break;
    case CLASSIFIERPRTMODEL:
        mModel = new ClassifierProductModel(1,columnName,this);
        mTableName = ((ClassifierProductModel*)mModel)->getTabName();
        setTitleName("产品管理");
        mTableView->setItemDelegate(new ClassifierPrtDelegate(this));
        break;
    default:
        break;
    }

    mModel->submit();
    ui->abcTv->setBarEnable(false);
    mTableView->setModel(mModel);

    QHeaderView* headerView = mTableView->verticalHeader();
#if QT_VERSION <0x050000
    headerView->setResizeMode(QHeaderView::ResizeToContents);
#else
    headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
#endif
    mTableView->setHHeadResizeMode(QHeaderView::Stretch);
    //mTableView->verticalHeader()->setDefaultSectionSize(100);

    connect(mTableView,SIGNAL(clicked(QModelIndex)),this,SLOT(selectItem(QModelIndex)));
    connect(mTableView,SIGNAL(longPressed(QModelIndex)),this,SLOT(openEditor(QModelIndex)));

    connect(ui->btn_PgDn,SIGNAL(clicked()),mTableView,SLOT(pageDown()));
    connect(ui->btn_PgUp,SIGNAL(clicked()),mTableView,SLOT(pageUp()));
    //connect(ui->btn_back,SIGNAL(clicked()),this,SLOT(quit()));

    connect(ui->serchEdit,SIGNAL(textChanged(QString)),this,SLOT(searchName(QString)));
    connect(ui->cBtn,SIGNAL(clicked()),this,SLOT(clearSearchText()));
}

EditWidget::~EditWidget()
{
    delete ui;
}

void EditWidget::on_btn_back_clicked()
{
    this->accept();
}

void EditWidget::searchName(const QString& str)
{
    if(mModelType==USERMODEL)
    {
        UserModel* model = (UserModel*)mModel;
        model->submitFilter(str);
    }
    else if(mModelType==PRODUCTMODEL)
    {
        ProductModel* model = (ProductModel*)mModel;
        model->submitFilter(str);
    }

}

void EditWidget::openEditor(const QModelIndex &index)
{
    BaseEditor *editor;
    if(mModelType==USERMODEL)
    {
        editor = new UserEditor(index,0);
    }
    else if(mModelType==PRODUCTMODEL)
    {
        editor = new ProductEditor(index,0);
    }
    else if(mModelType==CLASSIFIERPRTMODEL){
        editor = new ClassifierEditor(index,0);
    }
    connect(editor,SIGNAL(updateData()),this,SLOT(onUpdateDate()));
    editor->exec();
}

void EditWidget::onUpdateDate()
{
    mModel->submit();
}

void EditWidget::clearSearchText()
{
    ui->serchEdit->clear();
}

void EditWidget::selectItem(const QModelIndex &index)
{
    if(!index.data(ProductModel::ROLE_NAME).isNull())
    {
        emit selectedItem(index);
        this->accept();
    }
}

void EditWidget::setTitleName(const QString& name)
{
    ui->lab_title->setText(name);
}

void EditWidget::on_btn_new_clicked()
{
    BaseEditor *editor;
    if(mModelType==USERMODEL)
    {
        editor = new UserEditor();
    }
    else if(mModelType==PRODUCTMODEL)
    {
        editor = new ProductEditor();
    }
    else if (mModelType==CLASSIFIERPRTMODEL) {
        editor = new ClassifierEditor();
    }
    connect(editor,SIGNAL(updateData()),this,SLOT(onUpdateDate()));
    editor->exec();

}

void EditWidget::on_btn_del_clicked()
{
    TableItemDeleter* deleter = new TableItemDeleter(mTableName,mColumn);
    connect(deleter,SIGNAL(updateData()),this,SLOT(onUpdateDate()));
    deleter->exec();
}
